import 'package:library_base/entities/buyer_entity.dart';
import 'package:library_base/entities/task_detail_entity.dart';
import 'package:library_base/entities/tasks_entity.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/net/apis.dart';
import 'package:library_base/network/http_config.dart';
import 'package:library_base/provider/base_list_controller.dart';
import 'package:library_base/route/routers.dart';
import 'package:library_base/widget/multi_state_widget/multi_state_widget.dart';
import 'package:get/get.dart';

class TasksListController extends BaseListController<TasksEntity, TasksEntity> {
  /// 实体类
  RxList<TasksEntity> listDataSelected = <TasksEntity>[].obs;

  Rx<bool> isSelectAll = false.obs;

  Rxn<BuyerEntity?> consignor = Rxn<BuyerEntity?>();

  @override
  void onCreate() {
    enableLoadMore = false;
    isShowNoMoreFooter = false;
  }

  @override
  Future requestData() {
    isFirstLoading = true;
    requestGeneralListData(Apis.Tasks, isRefresh: false, method: FBResultFul.GET);
    return super.requestData();
  }

  @override
  Future onRefreshCallback() async {
    requestGeneralListData(Apis.Tasks, isRefresh: true, method: FBResultFul.GET);
  }

  void tryReLoad() {
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    baseState?.setStateCallBack?.call();
    requestGeneralListData(Apis.Tasks, isRefresh: false, method: FBResultFul.GET);
  }

  void tryReLoa1d() {
    requestGeneralListData(Apis.Tasks, isRefresh: true, method: FBResultFul.GET);
  }

  void onItemChildClick(TasksEntity itemData) {
    itemData.isSelect = !itemData.isSelect;
    if (!listDataSelected.value.contains(itemData)) {
      listDataSelected.value.add(itemData);
    } else {
      listDataSelected.value.remove(itemData);
    }
    isSelectAll.value = listDataSelected.length == listData.length;
    update();
  }

  void selectedAll() {
    if (listDataSelected.length != listData.length) {
      for (var element in listData.value) {
        element.isSelect = true;
      }
      listDataSelected.value.clear();
      listDataSelected.addAll(listData.value);
      isSelectAll.value = listDataSelected.length == listData.length;
      update();
    } else {
      for (var element in listData.value) {
        element.isSelect = false;
      }
      listDataSelected.value.clear();
      isSelectAll.value = listDataSelected.length == listData.length;
      update();
    }
  }

  void onItemClick(TasksEntity itemData) {
    requestUniversal<TaskDetailEntity>(Apis.TASKS_DETAIL,
        method: FBResultFul.GET,
        queryParameters: {'id': itemData.purchaseId}, resultSuccessCall: (taskDetailEntity) {
      Get.toNamed(Routers.createPurchaseOrderPage,
          arguments: {
        'TaskDetailEntity': taskDetailEntity,
      });
    });
  }

  @override
  List<TasksEntity> transformList(List<TasksEntity> dataList, bool isRefresh) {
    listDataSelected.value.clear();
    isSelectAll.value = false;
    return super.transformList(dataList, isRefresh);
  }


}
